home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <dos.h>
- #include <string.h>
- #include <direct.h>
- #include <malloc.h>
- #include "find.h"
-
- /* <*********************************************************************> */
- /* <* find.c 04/06/88 Peter Fitzsimmons *> */
- /* <*********************************************************************> */
- /* <* *> */
- /* <* *> */
- /* <* Compiler: Microsoft C Version 5.00, any memory model. *> */
- /* <* or: IBM C/2 Version 1.00, any memory model *> */
- /* <* *> */
- /* <*********************************************************************> */
- /* <* Notes: #define OS_2 for os/2 *> */
- /* <* - Right now, this code is either for DOS or OS/2, not *> */
- /* <* both. ( ie: You can't use it for a Family app ) *> */
- /* <*********************************************************************> */
- /* <* This module Copyright (C) 1988, 1989 Peter Fitzsimmons. *> */
- /* <* All rights reserved. *> */
- /* <*********************************************************************> */
-
- /* findtime() : this func writen just for Binkley, which combines the
- file date and file time into one long in certain situations.
- */
- long findtime(FSCAN *dh)
- {
- return( (long)dh->wr_date * 0x10000L + (long)dh->wr_time );
- }
-
-
- #ifndef OS_2 /* if not OS_2 assume MSC 5.00, for dos-only program */
-
- FSCAN *opendir(void)
- {
- FSCAN *retbuf;
-
- retbuf = malloc( sizeof(FSCAN) );
- if( retbuf ){
- retbuf->findhandle = malloc( sizeof(struct find_t) );
- if(retbuf->findhandle)
- retbuf->sig = FSCAN_SIG;
- else{
- free( retbuf );
- retbuf = NULL;
- }
- }
- return( retbuf );
- }
-
- void closedir( dh )
- FSCAN *dh;
- {
- if( dh->sig != FSCAN_SIG )
- puts( "closedir(): Invalid dir handle");
- else{
- free( dh->findhandle );
- free( dh );
- dh = NULL;
- }
- }
-
- findfirst( mask, attr, dh )
- char *mask;
- int attr;
- FSCAN *dh;
- {
- int retval = -1;
- struct find_t *dta;
- if( dh->sig != FSCAN_SIG )
- puts( "findfirst(): Invalid dir handle");
- else{
- dta = dh->findhandle;
- retval = _dos_findfirst( mask, attr, dta );
- if(!retval){
- strcpy(dh->name, dta->name);
- dh->attrib = dta->attrib;
- dh->wr_time = dta->wr_time;
- dh->wr_date = dta->wr_date;
- dh->size = dta->size;
- }
- }
- return( retval );
- }
-
- findnext( dh )
- FSCAN *dh;
- {
- int retval = -1;
- struct find_t *dta;
-
- if( dh->sig != FSCAN_SIG )
- puts( "findnext(): Invalid dir handle");
- else{
- dta = dh->findhandle;
- retval = _dos_findnext( dta );
- if(!retval){
- strcpy(dh->name, dta->name);
- dh->attrib = dta->attrib;
- dh->wr_time = dta->wr_time;
- dh->wr_date = dta->wr_date;
- dh->size = dta->size;
- }
- }
- return( retval );
- }
-
- #else /* OS_2 defined. Assume IBM C/2, and a protect mode only program */
-
- #define INCL_DOS
- #include <os2.h>
-
- FSCAN *opendir(void)
- {
- FSCAN *retbuf;
-
- retbuf = malloc( sizeof(FSCAN) );
- if( retbuf ){
- retbuf->findhandle = malloc( sizeof( HDIR ) );
- if(retbuf->findhandle)
- retbuf->sig = FSCAN_SIG;
- else{
- free( retbuf );
- retbuf = NULL;
- }
- }
- return( retbuf );
- }
-
- void closedir( dh )
- FSCAN *dh;
- {
- int stat;
- HDIR *DH;
-
- if( dh->sig != FSCAN_SIG )
- puts( "closedir(): Invalid dir handle");
- else{
- DH = dh->findhandle;
- stat = DosFindClose( *DH ); /* The OS/2 tech ref doesn't make it clear
- if you HAVE to do this. */
- free( dh->findhandle );
- free( dh );
- dh = NULL;
- }
- }
-
- findfirst( mask, attr, dh )
- char *mask;
- int attr;
- FSCAN *dh;
- {
- int retval = -1;
- HDIR *DH;
- int count = 1;
- static struct FileFindBuf ffb;
-
- if( dh->sig != FSCAN_SIG )
- puts( "findfirst(): Invalid dir handle");
- else{
- DH = dh->findhandle;
- *DH = 0xffff; /* request a dir handle */
- retval = DosFindFirst( mask, DH, attr, (PFILEFINDBUF)&ffb, sizeof( ffb ), (PUSHORT)&count, 0l);
- if(!retval){
- strcpy(dh->name, ffb.name);
- dh->attrib = (char)ffb.attrib;
- dh->wr_time = ffb.wr_time;
- dh->wr_date = ffb.wr_date;
- dh->size = ffb.size;
- }
- /*else if( retval != 18 )
- printf("find first error %u\n", retval );*/
- }
- return( retval );
- }
-
- findnext( dh )
- FSCAN *dh;
- {
- int retval = -1;
- HDIR *DH;
- int count = 1;
- static struct FileFindBuf ffb;
-
- if( dh->sig != FSCAN_SIG )
- puts( "findfirst(): Invalid dir handle");
- else{
- DH = dh->findhandle;
- retval = DosFindNext( *DH, (PFILEFINDBUF)&ffb, sizeof( ffb ), (PUSHORT)&count);
- if(!retval){
- strcpy(dh->name, ffb.name);
- dh->attrib = (char)ffb.attrib;
- dh->wr_time = ffb.wr_time;
- dh->wr_date = ffb.wr_date;
- dh->size = ffb.size;
- }
- /*else if( retval != 18 )
- printf("find next error %u\n", retval );*/
- }
- return( retval );
- }
-
- #endif /* OS_2 */
-
-
- #ifdef TEST_SHELL
-
- int walk(char *path);
-
- void main(int argc, char **argv)
- {
- walk("\\"); // start at root
- }
-
- /* this simple function assumes the path ALWAYS has an ending backslash */
- walk(char *path)
- {
- FSCAN *dh;
- int done = FALSE;
- char full[66];
-
- strcpy(full, path);
- strcat(full, "*.*");
- if( dh = opendir() ){
- for( done = findfirst(full, _A_SUBDIR, dh); !done; done = findnext(dh)){
- if( (dh->attrib & _A_SUBDIR) && (dh->name[0] != '.') ){
- strcpy(full, path);
- strcat(full, dh->name);
- puts(full);
- strcat(full, "\\");
- if( !walk(full) )
- return(FALSE);
- }
- }
- closedir(dh);
- return(TRUE);
- }
- else{
- puts("opendir() failed");
- }
- return(FALSE);
- }
-
- #endif
-
-